package com.compass.doctor.ui.home.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.databinding.FragmentRoutineDrugBinding
import com.compass.doctor.ui.home.MedicalCareOrderActivity
import com.compass.doctor.ui.home.adapter.CareExecutedAdapter
import com.compass.doctor.ui.home.viewmodel.MedicalCareViewModel
import com.compass.framework.constant.REFRESH_CARE
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.LiveDataBus
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 医护上门/已执行
 * Date:2023/12/11 21:07
 */
class CareExecutedFragment: BaseVBFragment<FragmentRoutineDrugBinding>(), OnRefreshListener,
    OnLoadMoreListener {
    private var mPage = 1
    private val medicalCareViewModel: MedicalCareViewModel by viewModels()
    private lateinit var careExecutedAdapter: CareExecutedAdapter
    override fun initView(view: View, savedInstanceState: Bundle?) {
        careExecutedAdapter = CareExecutedAdapter(1)

        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@CareExecutedFragment)
            setOnLoadMoreListener(this@CareExecutedFragment)
        }
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = careExecutedAdapter

        }

        careExecutedAdapter.onItemClickListener = { view: View, position: Int ->
            val item = careExecutedAdapter.getItem(position)
            item?.let {
                MedicalCareOrderActivity.start(requireContext(), item.id)
            }

        }
    }

    override fun initData() {
        getDataList ()
        LiveDataBus.get().with(REFRESH_CARE, String::class.java).observe(this){
            getDataList()
        }
        medicalCareViewModel.careLiveData.observe(this){
            dismissLoading()

            if (mPage == 1) {
                careExecutedAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.viewEmpty?.visible()
                } else {
                    mBinding?.viewEmpty?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    careExecutedAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }

        }
    }

    private fun getDataList() {
        showLoading()
        medicalCareViewModel.getCareNurseList(mPage)
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getDataList()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getDataList()
    }
}